package com.wsc.wscbirthdaykeeper.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import com.wsc.wscbirthdaykeeper.R;
import com.wsc.wscbirthdaykeeper.managers.ResourceManager;
import com.wsc.wscbirthdaykeeper.model.KRuntimeModel;
import com.wsc.wscbirthdaykeeper.model.entity.Person;
import com.wsc.wscbirthdaykeeper.utils.DateUtils;
import com.wsc.wscbirthdaykeeper.utils.DialogUtils;

public class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory {
	private Context context;
	private KRuntimeModel runtime = KRuntimeModel.getInstance();

	public WidgetViewsFactory(Context ctxt, Intent intent) {
		this.context = ctxt;
	}

	@Override
	public void onCreate() {
	}

	@Override
	public void onDestroy() {
	}

	@Override
	public int getCount() {
		int count = 0;
		try {
			count = runtime.getPersons().size();
		} catch (Exception e) {
			DialogUtils.showException(context, "WidgetViewsFactory::getCount", e);
		}

		return count;
	}

	@Override
	public RemoteViews getViewAt(int position) {
		RemoteViews row = null;
		try {

			Person person = runtime.getPersons().get(position);

			row = new RemoteViews(context.getPackageName(), R.layout.item_widget);
			row.setTextViewText(R.id.name, person.getName());

			int days = DateUtils.getWaitDays(person.getDate());

			switch (days) {
			case 0:
				row.setTextViewText(R.id.date, ResourceManager.getStringValue(R.string.person_today));
				row.setTextColor(R.id.date, Color.GREEN);
				break;
			case 1:
				row.setTextViewText(R.id.date, ResourceManager.getStringValue(R.string.person_tomorrow));
				row.setTextColor(R.id.date, Color.GREEN);
				break;
			default:
				row.setTextViewText(R.id.date, ResourceManager.getStringValue(R.string.person_through) + " " + days + " " + ResourceManager.getStringValue(R.string.person_days));
				row.setTextColor(R.id.date, Color.WHITE);
			}

			Intent i = new Intent();
		//	Bundle extras = new Bundle();

		//	extras.putString(WidgetProvider.EXTRA_WORD, person.getName());
		//	i.putExtras(extras);
			row.setOnClickFillInIntent(R.id.name, i);

		} catch (Exception e) {
			DialogUtils.showException(context, "WidgetViewsFactory::getViewAt", e);
		}
		return row;
	}

	@Override
	public RemoteViews getLoadingView() {
		return null;
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public void onDataSetChanged() {
	}
}